package com.hytech.utilities;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class EULAActivity extends Activity {

	
	static final String PREF_EULA_ACCEPT = "eula_accepted";
	
	// this is our real package and class. 
	private String mRealPackage;
	private String mRealClass;
	
	private static final String MAIN_ACTIVITY_PACKAGE = "Activity Package";
	private static final String MAIN_ACTIVITY_CLASS = "Activity Class";
	
	private static int mEULAid;
	
	private Button mAgree;
	private Button mDisagree;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle incomingBundle) {
		super.onCreate(incomingBundle);
		
		setContentView(R.layout.eulalayout);
		
		// Extras are provided by checkEula() below.
		Intent i = getIntent();
		Bundle b = i.getExtras();
		mRealPackage = b.getString(MAIN_ACTIVITY_PACKAGE);
		mRealClass = b.getString(MAIN_ACTIVITY_CLASS);
		
		//mIntroContinue = (Button) findViewById(R.id.intro_continue);
		mAgree = (Button) findViewById(R.id.EULAAcceptButton);
		mAgree.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				onAcceptEula();
			}
		});
		
		mDisagree = (Button) findViewById(R.id.EULARejectButton);
		mDisagree.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				onRefuseEula();
			}
		});
		
		TextView text = (TextView) findViewById(R.id.EULAText);
		text.setText(readEULAFromRawFile());
		
	}
	
	/**
	 * Accept EULA and proceed with main application.
	 */
	public void onAcceptEula() {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor e = sp.edit();
		e.putBoolean(PREF_EULA_ACCEPT, true);
		e.commit();
		
		// Call the activity that originally called checkEula()
		Intent i = new Intent();
		i.setClassName(mRealPackage, mRealClass);
		startActivity(i);
		finish();
	}
	
	/**
	 * Refuse EULA.
	 */
	public void onRefuseEula() {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor e = sp.edit();
		e.putBoolean(PREF_EULA_ACCEPT, false);
		e.commit();
		
		finish();
	}

	/**
	 * Test whether EULA has been accepted. Otherwise display EULA.
	 * 
	 * @return True if Eula has been accepted.
	 */
	public static boolean checkEula(Activity activity, int EULAid) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
		boolean accepted = sp.getBoolean(PREF_EULA_ACCEPT, false);
		
		if ( accepted ) {
			return true;
		} else {
			
			// Launch Eula activity
			Intent i = new Intent(activity, EULAActivity.class);
			ComponentName ci = activity.getComponentName();
			i.putExtra(MAIN_ACTIVITY_PACKAGE, ci.getPackageName());
			i.putExtra(MAIN_ACTIVITY_CLASS, ci.getClassName());
			mEULAid = EULAid;
			activity.startActivity(i);
			activity.finish();
			return false;
		}
	}
	
	/**
	 * Read license from raw resource.
	 * @param resourceid ID of the raw resource.
	 * @return
	 */
	private String readEULAFromRawFile() {
		// Retrieve license from resource:
		String licensetext = "";
		Resources resources = getResources();
		
		// Grab the EULA as a reader.
		BufferedReader in
		   = new BufferedReader(new InputStreamReader(
				resources.openRawResource(mEULAid)));
		
		// current line string.
		String line;
		
		// string builder system so we can build the license text 
		StringBuilder sb = new StringBuilder();
		try {
			
			// for each line in the file. 
			while ((line = in.readLine()) != null) {
			
				// if there's data in the line.
				if (TextUtils.isEmpty(line)) {
					// Empty line: Leave line break
					sb.append("\n\n");
				} else {
					// append the line
					sb.append(line);
					sb.append(" ");
				}
			}
			// licensetext
			licensetext = sb.toString();
		} catch (IOException e) {
			//Should not happen.
			e.printStackTrace();
		}
    	
    	return licensetext;
	}
}